package com.hhkj.admin.sale.service.base.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.sale.domain.Sale;
import com.hhkj.admin.sale.mapper.SaleMapper;
import com.hhkj.admin.sale.model.dopo.SaleDo;
import com.hhkj.admin.sale.model.dopo.SaleManagementPo;
import com.hhkj.admin.sale.model.dopo.SalePo;
import com.hhkj.admin.sale.service.base.SaleService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import com.ruoyi.common.cont.SysConst;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author admin
 */
@Service
public class SaleServiceImpl extends BaseServiceImpl<Sale, SaleDo, SalePo, SaleMapper>
        implements SaleService {

    @Override
    public boolean save(Sale entity) {
        entity.setDisabledFlag(SysConst.Flag.NO.getFlag());
        return super.save(entity);
    }

    @Override
    public Long countByLevel(String level) {
        LambdaQueryWrapper<Sale> saleQuery = new LambdaQueryWrapper<>();
        saleQuery.eq(Sale::getLevel, level);
        return this.count(saleQuery);
    }

    @Override
    public Sale getByNum(String num) {
        LambdaQueryWrapper<Sale> saleQuery = new LambdaQueryWrapper<>();
        saleQuery.eq(Sale::getNum, num)
                .orderByDesc(Sale::getCreateTime);
        return this.getOne(saleQuery, false);
    }

    @Override
    public Sale getByMobile(String mobile) {
        LambdaQueryWrapper<Sale> saleQuery = new LambdaQueryWrapper<>();
        saleQuery.eq(Sale::getMobile, mobile)
                .orderByDesc(Sale::getCreateTime);
        return this.getOne(saleQuery, false);
    }

    @Override
    public long countByDo(SaleDo saleDo) {
        if (ObjectUtil.isEmpty(saleDo)) {
            return 0;
        }
        LambdaQueryWrapper<Sale> queryWrapper = buildDoWrapper(saleDo);
        return count(queryWrapper);
    }

    @Override
    public List<SaleManagementPo> listForManagement(SaleDo saleDo) {
        return getBaseMapper().listForManagement(saleDo);
    }

    private LambdaQueryWrapper<Sale> buildDoWrapper(SaleDo saleDo) {
        LambdaQueryWrapper<Sale> queryWrapper = new LambdaQueryWrapper<Sale>()
                .like(ObjectUtil.isNotEmpty(saleDo.getName()), Sale::getName, saleDo.getName())
                .eq(ObjectUtil.isNotEmpty(saleDo.getMobile()), Sale::getMobile, saleDo.getMobile())
                .eq(ObjectUtil.isNotEmpty(saleDo.getLevel()), Sale::getLevel, saleDo.getLevel())
                .like(ObjectUtil.isNotEmpty(saleDo.getNum()), Sale::getNum, saleDo.getNum())
                .eq(ObjectUtil.isNotEmpty(saleDo.getIdCard()), Sale::getIdCard, saleDo.getIdCard())
                .eq(ObjectUtil.isNotEmpty(saleDo.getTypeCode()), Sale::getTypeCode, saleDo.getTypeCode())
                .eq(ObjectUtil.isNotEmpty(saleDo.getDisabledFlag()), Sale::getDisabledFlag, saleDo.getDisabledFlag())
                .eq(ObjectUtil.isNotEmpty(saleDo.getParentId()), Sale::getParentId, saleDo.getParentId())
                .eq(ObjectUtil.isNotEmpty(saleDo.getRootId()), Sale::getRootId, saleDo.getRootId());
        if (ObjectUtil.isNotEmpty(saleDo.getRegisterDateStart()) && ObjectUtil.isNotEmpty(saleDo.getRegisterDateEnd())) {
            queryWrapper.ge(Sale::getRegisterTime, saleDo.getRegisterDateStart())
                    .le(Sale::getRegisterTime, saleDo.getRegisterDateEnd());
        }
        return queryWrapper;
    }
}




